//
//  WeiXinApiManager.m
//  KwSing
//
//  Created by yuchenghai on 14-9-30.
//  Copyright (c) 2014年 kuwo.cn. All rights reserved.
//

#import "WeiXinApiManager.h"
#import "WXApi.h"
#import "SGAppDelegate.h"

@interface WeiXinApiManager () <WXApiDelegate>

@end


@implementation WeiXinApiManager

+ (id)defaultManager {
    static WeiXinApiManager *weixinMgr = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        weixinMgr = [[WeiXinApiManager alloc] init];
    });
    return weixinMgr;
}

- (id)init {
    self = [super init];
    if (self) {
        [[SGAppDelegate appDelegate] setWxApiDelegate:self];
    }
    return self;
}

- (void)shareToWeixinInAPP:(KSShareToWXType)shareToWXType
                     title:(NSString *)title
                       url:(NSString *)url
                   dataUrl:(NSString *)dataUrl
               description:(NSString *)description
                     image:(UIImage *)image
                 toFriends:(BOOL)toFriends
            withCompletion:(shareApiCompletionBlock)completion
{
    WXMediaMessage *sendMsg = [WXMediaMessage message];
    
    sendMsg.title = title;
    sendMsg.description = description;

    NSLog(@"title:%@", title);
    NSLog(@"desc:%@", description);
    NSLog(@"url:%@", url);
    [sendMsg setThumbImage:image];

    if (shareToWXType == KSShareToWXTypeVideo) {
        WXVideoObject *ext = [WXVideoObject object];
        ext.videoUrl = url;
        
        sendMsg.mediaObject = ext;
    } else if (shareToWXType == KSShareToWXTypeMusic) {
        WXMusicObject *ext = [WXMusicObject object];
        /** 音乐网页的url地址
         * @attention 长度不能超过10K
         */
        ext.musicUrl = url;
        //    NSLog(@"share url:%@",[self playUrl]);
        /** 音乐数据url地址
         * @attention 长度不能超过10K
         */
        ext.musicDataUrl = dataUrl;
        
        sendMsg.mediaObject = ext;
    } else if (shareToWXType == KSShareToWXTypeWebPage) {
        // 分享网页
        WXWebpageObject *ext = [WXWebpageObject object];
        ext.webpageUrl = url;
        
        sendMsg.mediaObject = ext;
    } else if (shareToWXType == KSShareToWXTypeImage) {
        
        WXImageObject *ext = [WXImageObject object];
        ext.imageData = UIImageJPEGRepresentation(image, 1.);
        
        sendMsg.mediaObject = ext;
    }
    
    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    req.message = sendMsg;
    req.bText = false;
    if (toFriends) {
        req.scene = WXSceneSession;  // 发送给好友
    } else {
        req.scene = WXSceneTimeline; // 发送给朋友圈
    }
    [WXApi sendReq:req];
    
    _shareCompletionBlock = [completion copy];
}


#pragma mark WXApiDelegate
- (void)onResp:(id)resp {
    if ([resp errCode] != 0) {
        NSLog(@"分享失败了!");
        if (_shareCompletionBlock) {
            _shareCompletionBlock(NO);
            _shareCompletionBlock = nil;
        }

        return;
    }
    
    if (_shareCompletionBlock) {
        _shareCompletionBlock(YES);
        _shareCompletionBlock = nil;
    }
}

-(void) onReq:(BaseReq*)req {
    
}

@end
